Skip to content

feat(decorators): support RegExp instances in @ApiProperty({ pattern })#3787

Open
temrjan wants to merge 2 commits intonestjs:masterfrom
temrjan:feat/support-regexp-pattern-in-api-property
Open

feat(decorators): support RegExp instances in @ApiProperty({ pattern })#3787
temrjan wants to merge 2 commits intonestjs:masterfrom
temrjan:feat/support-regexp-pattern-in-api-property

Conversation

@temrjan
Copy link
Copy Markdown

@temrjan temrjan commented Mar 28, 2026

Summary

  • Automatically convert RegExp instances to their .source string when passed to @ApiProperty({ pattern })
  • Extend SchemaObjectCommonMetadata to accept pattern?: string | RegExp
  • Fully backward compatible — string values work as before

Test plan

  • All 158 tests pass (16 suites)
  • Build (tsc) passes with no new errors
  • Zero new any types introduced
  • Edge cases verified: null, undefined, string, empty RegExp — all safe via instanceof RegExp

Fixes #3719

🤖 Generated with Claude Code

Automatically extract .source from RegExp values passed to the pattern
option, so developers can use @ApiProperty({ pattern: /^[a-z]+$/ })
instead of manually calling .source.

Fixes nestjs#3719

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves @ApiProperty({ pattern }) developer ergonomics by accepting RegExp instances and ensuring they serialize into the OpenAPI schema as a string pattern.

Changes:

  • Extend SchemaObjectCommonMetadata to allow pattern?: string | RegExp.
  • In createApiPropertyDecorator, convert options.pattern from RegExp to its .source string before storing metadata.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lib/interfaces/schema-object-metadata.interface.ts Widen pattern typing to support RegExp while keeping OpenAPI SchemaObject compatibility via Omit<..., 'pattern'>.
lib/decorators/api-property.decorator.ts Add runtime normalization so RegExp patterns are stored as strings in decorator metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +87 to +89
if (options.pattern instanceof RegExp) {
options.pattern = options.pattern.source;
}
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds new runtime behavior (converting options.pattern when it’s a RegExp), but there doesn’t appear to be a test asserting the generated schema’s pattern value. Please add a test that defines a DTO property with @ApiProperty({ pattern: /^[+]?abc$/ }) (and ideally one with flags, e.g. /abc/i) and verifies the resulting OpenAPI schema contains pattern: '^[+]?abc$' (and that flags are not emitted).

Copilot uses AI. Check for mistakes.
Cover RegExp-to-string conversion, flag stripping, and string passthrough
in the OpenAPI schema generated by SchemaObjectFactory.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support RegExp instances in @ApiProperty({ pattern })

2 participants